"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，跑tpcc时, 查询资源池化的线程级别内存统计信息
Create At   : 2025-05-09
Owner       : huzhengchao
Description :
    1、查询集群状态
    2、启动tpcc
    3、查询 gs_thread_memory_context 内的 DMSMemContext 信息

Expect:
    1、查询结果正常
    2、tpcc启动成功
    3、查询 gs_thread_memory_context 得到 DMSMemContext 信息
History     :
    1、2025-05-29 创建
"""
import os
import time
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.tpcc_user = Node('PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:启动tpcc;-----'
        self.log.info(text)
        start_tpcc = ComThread(self.com.start_tpcc,
                               args=(self.tpcc_user,'/data/benchmarksql-5.0/run',
                               './runBenchmark.sh props_write.pg'))
        start_tpcc.setDaemon(True)
        start_tpcc.start()
        
        text = '-----step3:gs_thread_memory_context; expect:数据不为0-----'
        self.log.info(text)
        time.sleep(20)
        sql = "select case when count(*) > 0 then 'yes' else 'no' end as have_info " + \
              "from gs_thread_memory_context where contextname = 'DMSMemContext';"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('yes', status, '执行失败：查询不到DMSMemContext内存上下文的信息。' + text)
        
        
    def tearDown(self):
        text = '-----恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        